<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>缓动的小球</title>
    <style>
        #box{
            position:absolute;
            width: 120px;
            height: 120px;
            line-height: 120px;
            text-align: center;
            background-color: red;
            color: #ffffff;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div id="box">点我啊，快跑！</div>  
</body>
<script>
     function animate(obj,option){
         clearInterval.
        obj.timer = setInterval(function(){
            var flag = true;
            for(var k in option){
              var leader = getStyle(obj,k) || 0;//获取指定元素当前属性值
              var target = option[k];//获取指定元素某标属性值
              var stop = (target - leader) / 10;//计算每次移动的步长
              stop = stop > 0 ? Math.ceil(stop) : Math.floor(stop);
              leader = leader + stop;//计算属性值
              obj.style[k] = leader+"px";//设置属性值
              if(leader != target){
                  flag = false;
              }
            }
            if(flag){
                clearInterval(obj.timer)
            }


        },2);
     }
    
     function getElementById(obj,attr){
         if(window.getComputedStyle){
             return window.getComputedStyle(obj,null)[attr];
         }else{
             return obj,currentStyle[attr];
         }
     }


    var obj = document.getElementById('box');
    obj.onclick = function(){
        animate(obj,{"left":500,"top":200});  
    };
</script>
</html>